package com.xl6667.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xl6667.constants.SystemConstants;
import com.xl6667.domain.ResponseResult;
import com.xl6667.domain.dto.AddRoel;
import com.xl6667.domain.dto.PageDto;
import com.xl6667.domain.dto.RoleDto;
import com.xl6667.domain.entity.Role;
import com.xl6667.domain.entity.RoleMenu;
import com.xl6667.mapper.RoleMapper;
import com.xl6667.service.MenuService;
import com.xl6667.service.RoleMenuService;
import com.xl6667.service.RoleService;
import com.xl6667.util.BeanCopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 熊磊
* @description 针对表【sys_role(角色信息表)】的数据库操作Service实现
* @createDate 2024-03-21 13:17:30
*/
@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        if (id == 1L){
            List<String> roleKeys = new ArrayList<>();
            roleKeys.add("admin");
            return roleKeys;
        }

        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public ResponseResult getRoleList(Integer pageNum, Integer pageSize, String roleName, String status) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(roleName),Role::getRoleName,roleName);
        queryWrapper.eq(StringUtils.hasText(status),Role::getStatus,status);
        queryWrapper.eq(Role::getDelFlag, SystemConstants.DEL_FLAG_NORMA);

        Page<Role> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);

        List<Role> roleList = page.getRecords();
        List<RoleDto> roleDtos = BeanCopyUtil.copyBeanList(roleList, RoleDto.class);
        PageDto pageDto = new PageDto(roleDtos,page.getTotal());
        return ResponseResult.okResult(pageDto);
    }

    @Override
    public ResponseResult changeStatusRole(Role role) {
        updateById(role);
        return ResponseResult.okResult("修改成功");
    }

    @Override
    @Transactional
    public ResponseResult addRole(AddRoel addRoel) {
        Role role = BeanCopyUtil.copyBean(addRoel, Role.class);
        save(role);

        List<Long> menuIds = addRoel.getMenuIds();
        log.info(role.getId().toString());
        List<RoleMenu> roleMenus = menuIds.stream().map(menuId -> new RoleMenu(role.getId(), menuId)).collect(Collectors.toList());
        roleMenuService.saveBatch(roleMenus);
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult getRole(Long id) {
        Role role = getById(id);
        RoleDto roleDto = BeanCopyUtil.copyBean(role, RoleDto.class);
        return ResponseResult.okResult(roleDto);
    }

    @Override
    @Transactional
    public ResponseResult putRole(AddRoel addRoel) {
        Role role = BeanCopyUtil.copyBean(addRoel, Role.class);
        updateById(role);


        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getRoleId,role.getId());
        roleMenuService.remove(queryWrapper);

        LambdaQueryWrapper<RoleMenu> queryWrapper1 = new LambdaQueryWrapper<>();
        List<RoleMenu> roleMenus = addRoel.getMenuIds().stream().map(id -> new RoleMenu(role.getId(), id)).collect(Collectors.toList());
        roleMenuService.saveBatch(roleMenus);
        return ResponseResult.okResult("修改成功");

    }

    @Override
    public ResponseResult delRole(Long id) {
        LambdaUpdateWrapper<Role> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Role::getId,id).set(Role::getDelFlag,SystemConstants.DEL_FLAG);
        update(null,updateWrapper);
        return ResponseResult.okResult("删除成功");
    }

    @Override
    public ResponseResult getListAllRole() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getDelFlag,SystemConstants.DEL_FLAG_NORMA);
        queryWrapper.eq(Role::getStatus,SystemConstants.STATUS_NORMA);

        List<Role> roleList = list(queryWrapper);
        List<RoleDto> roleDtos = BeanCopyUtil.copyBeanList(roleList, RoleDto.class);
        return ResponseResult.okResult(roleDtos);
    }

}




